package org.pb.pet.service.impl;

import org.pb.basic.service.impl.BaseServiceImpl;
import org.pb.basic.utils.LoginContextUtils;
import org.pb.org.domain.Employee;
import org.pb.org.mapper.EmployeeMapper;
import org.pb.pet.domain.Pet;
import org.pb.pet.domain.PetDetail;
import org.pb.pet.mapper.PetDetailMapper;
import org.pb.pet.mapper.PetMapper;
import org.pb.pet.query.PetQuery;
import org.pb.pet.service.IPetService;
import org.pb.user.domain.LoginInfo;
import org.pb.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.security.auth.login.LoginContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet, PetQuery> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void save(Pet condition) {
        // 先保存pet信息，获取到对应宠物的自增长主键
        super.save(condition);
        // 然后将宠物id、预约提示、宠物描述添加到宠物明细中
        PetDetail petDetail = condition.getDetail();
        petDetail.setPet_id(condition.getId());
        petDetailMapper.save(petDetail);
    }

    @Override
    @Transactional
    public void update(Pet condition) {
        // 根据修改后的宠物信息修改到pet表中
        super.update(condition);
        // 给condition中的detail设置pet_id
        condition.getDetail().setPet_id(condition.getId());
        // 同步修改宠物明细中的内容
        petDetailMapper.updateByPetId(condition.getDetail());
    }

    @Override
    @Transactional
    public void remove(Long id) {
        // 先根据id删除对应的宠物信息
        super.remove(id);
        // 然后根据petId删除对应的宠物明细信息
        petDetailMapper.removeByPetId(id);
    }

    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {
        // 先批量删除宠物信息
        super.batchRemove(ids);
        // 然后批量删除宠物明细信息
        petDetailMapper.batchRemoveByPetId(ids);
    }

    @Override
    public void onSale(List<Long> ids) {
        // 参数的非空判断
        if (ids == null || ids.size() <= 0) {
            throw new RuntimeException("请选择至少一个宠物进行操作");
        }
        // 如果有参数直接修改对应的上架时间和对应上下架状态【state = 1】
        petMapper.batchOnSale(ids, new Date());
    }

    @Override
    public void offSale(List<Long> ids) {
        // 参数的非空判断
        if (ids == null || ids.size() <= 0) {
            throw new RuntimeException("请选择至少一个宠物进行操作");
        }
        // 如果有参数直接修改对应的下架时间和对应的上下架状态【state = 0】
        petMapper.batchOffSale(ids, new Date());
    }

    @Override
    public void adopt(Long id, HttpServletRequest request) {
        if (id == null) {
            throw new RuntimeException("宠物参数异常");
        }
        // 通过id获取到当前宠物的信息
        Pet pet = petMapper.loadById(id);
        // 通过请求对象获取到当前领养用户的信息
        LoginInfo loginInfo = LoginContextUtils.getLoginInfo(request);
        // 通过当前loginInfo对象的id获取到对应对象的信息
        Long user_id = userMapper.getByLoginInfo(loginInfo);
        // 将user_id设置到一个pet参数对象中
        pet.setUser_id(user_id);
        pet.setId(id);
        // 设置下架时间
        pet.setOffsaletime(new Date());
        // 状态修改为下架
        pet.setState(0);
        // 执行修改方法
        petMapper.update(pet);
    }

    @Override
    public void save(Pet pet, HttpServletRequest request) {
        // 通过工具类获取到当前的登录用户信息
        LoginInfo loginInfo = LoginContextUtils.getLoginInfo(request);
        // 通过loginInfo对象获取到对应的店铺信息
        Employee shoper = employeeMapper.getByLogInfoId(loginInfo);
        System.err.println(shoper);
        // 将店铺id设置到对应的宠物参数中
        pet.setShop_id(shoper.getShop_id());
        // 先保存宠物信息
        petMapper.save(pet);
        // 通过自增主键获取到pet的id，将其赋值给pet中的detail属性的pet_id属性
        pet.getDetail().setPet_id(pet.getId());
        // 然后保存宠物明细信息
        petDetailMapper.save(pet.getDetail());
    }
}
